﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Grizzy_X
{
    public class CPoint
    {
        private int ID;
        
        public List<PointF> points;
        public MBR MinBR;
        public int LegendID;
        public bool isselected;
        public int GetID()
        {
            return ID;
        }

        public void newCpoint(int ptid,int legdid)
        {
            ID = ptid;
            LegendID = legdid;           
            points = new List<PointF>();
            MinBR = new MBR();
            isselected = false;
        }

        public void AddPoint(PointF p1)
        {
            points.Add(p1);
        }
        public void DeletePoint()
        {

        }
        public void deletesimplepoint()
        {
            for (int i = 0; i < Controller.selectsimple.Count(); i++)
            {
                points.RemoveAt(Controller.selectsimple[i]);
                points.Insert(Controller.selectsimple[i],new PointF(-1,-1));
            }
            for (int i = 0; i < points.Count(); i++)
            {
                if (points[i].X == -1)
                {
                    points.RemoveAt(i);
                    i--;
                }
            }
        }
        public void DragPoint(Point movepoint)
        {
            for (int i = 0; i < points.Count(); i++)
            {
                Point screen = new Point();
                screen = Transformer.ToScreen(points[i]);
                screen.X = screen.X + movepoint.X;
                screen.Y = screen.Y + movepoint.Y;
                PointF temp = Transformer.FromScreen(screen);
                //PointF temp = new PointF((points[i].X + movepoint.X), (points[i].Y + movepoint.Y));
                points.RemoveAt(i);
                points.Insert(i,temp);
                /*points[i].X = temp;
                points[i].Y += movepoint.Y;*/
            }
        }
        public void DragsimplePoint(Point movepoint)
        {
            for (int i = 0; i < Controller.selectsimple.Count(); i++)
            {
                Point screen = new Point();
                screen = Transformer.ToScreen(points[Controller.selectsimple[i]]);
                screen.X = screen.X + movepoint.X;
                screen.Y = screen.Y + movepoint.Y;
                PointF temp = Transformer.FromScreen(screen);
                //PointF temp = new PointF((points[i].X + movepoint.X), (points[i].Y + movepoint.Y));
                points.RemoveAt(Controller.selectsimple[i]);
                points.Insert(Controller.selectsimple[i], temp);
                /*points[i].X = temp;
                points[i].Y += movepoint.Y;*/
            }
        }
        public void updateMBR()
        {
            if (points.Count() > 0)
            {
                MinBR.GetMBR(points);
                float trans;
                if (Controller.MyMap.Unit == "英寸")
                    trans = (float)1 / 96;
                else
                    trans = (float)2.54 / 96;
                CPointLegend legend = (CPointLegend)Controller.MapLegends[LegendID];
                float buffer = legend.size * trans / 2;
                MinBR.Lx = MinBR.Lx - buffer;
                MinBR.Ly = MinBR.Ly - buffer;
                MinBR.Rx = MinBR.Rx + buffer;
                MinBR.Ry = MinBR.Ry + buffer;
            }
        }
        public void selectsimplepoint(PointF e)
        {
            float trans;
            if (Controller.MyMap.Unit == "英寸")
                trans = (float)1 / 96;
            else
                trans = (float)2.54 / 96;
            float Buffer = Math.Max(((CPointLegend)Controller.MapLegends[LegendID]).size * trans / 2, 10 * trans);
            if (MinBR.Lx - Buffer < e.X &&
                MinBR.Ly - Buffer < e.Y &&
               MinBR.Rx + Buffer > e.X &&
                MinBR.Ry + Buffer > e.Y)
            {
                for (int j = 0; j < points.Count(); j++)
                {
                    if (Math.Abs(points[j].X - e.X) < Buffer
                        && Math.Abs(points[j].Y - e.Y) < Buffer)
                    {
                        for (int m = 0; m < Controller.selectsimple.Count(); m++)
                        {
                            if (j == Controller.selectsimple[m])
                            {
                                Controller.selectsimple.RemoveAt(m);
                                return;
                            }
                        }
                        Controller.selectsimple.Add(j);
                        return;
                    }
                }
            }
        }
 
    }
}
